<?php
/**
 *
 */
class terminal extends CI_Controller {

  public function __construct() {
    parent::__construct();
    $this->load->model('M_terminal');
    $this->load->helper('url');
    $this->load->helper('form');
    // $this->load->library('form_validation');
    $this->load->library('session');
    if($this->session->userdata('username')==null){
      redirect('/login');
    }
  }
  public function terminallist(){
    $this->load->library('pagination');
    $page = 0;
    $offset = 0;
    $per_page = 10;
    if(!empty($_GET['per_page'])){
      $page = $_GET['per_page'];
      $offset = ($page-1)*$per_page;
    }
    $result = $this->M_terminal->getTerminallist($per_page,$offset);
    if(!empty($_GET['search'])){
      // $result = $this->M_shop->selectShop($_GET['search'],$per_page,$offset);
    }
    $config['base_url']=site_url('terminal/terminallist');
    $config['total_rows'] = $result['total'];
    $config['per_page'] = $per_page;
    $this->pagination->initialize($config);
    $data['terminallist'] = $result['terminallist'];
    $data['pager'] = $this->pagination->create_links();
    $this->load->view('terminallist',$data);
  }
  public function terminaldetail(){
    if(empty($_GET['teid'])){

    }else{
      $teid = $_GET['teid'];
      $this->load->library('pagination');
      $page = 0;
      $offset = 0;
      $per_page = 5;
      if(!empty($_GET['per_page'])){
        $page = $_GET['per_page'];
        $offset = ($page-1)*$per_page;
      }
      $result = $this->M_terminal->getTerminalDetail($teid,$per_page,$offset);
      if($result['flag']==0){
        redirect('/shop/shop_list');
      }
      $config['base_url']=site_url('terminal/terminaldetail');
      $config['total_rows'] = $result['total'];
      $config['per_page'] = $per_page;
      $this->pagination->initialize($config);
      $data['terminal'] = $result['terminaldetail'];
      $data['callback'] = $result['callback'];
      $data['teid'] = $teid;
      $data['pager'] = $this->pagination->create_links();
      $this->load->view('terminaldetail',$data);
      // $result = $this->M_shop->getShopDetail($shopid);
    }
  }
  public function terminaladd(){
    if($this->session->userdata('username')!='admin'){
      redirect('/home');
    }
    $this->load->view('terminaladd');
  }
  public function addTerminal(){
    if(!empty($_POST['tename']) && !empty($_POST['tetel']) && !empty($_POST['tebirth']) && !empty($_POST['shopid']) && !empty($_POST['bid']) && !empty($_POST['teteacher'])){
      $option = array(
        'tename'        => $_POST['tename'],
        'tetel'         => $_POST['tetel'],
        'tebirth'       => $_POST['tebirth'],
        'shopid'        => $_POST['shopid'],
        'bid'           => $_POST['bid'],
        'shopname'        => $_POST['shopname'],
        'bname'           => $_POST['bname'],
        'teteacher'       => $_POST['teteacher']
      );
      // echo $this->M_terminal->addTerminal($option);
      print_r($this->M_terminal->addTerminal($option));
    }
  }
  public function terminaldelete(){
    if(!empty($_POST['teid']) && $this->session->userdata('username')=='admin'){
      return $this->M_terminal->deleteTerminal($_POST['teid']);
    }else{
      echo 'false';
    }
  }
  /**
  *修改终端接口
  */
  public function updateTerminal(){
    if(!empty($_POST['teid']) && !empty($_POST['tename']) && !empty($_POST['tetel']) && !empty($_POST['tebirth']) && !empty($_POST['shopid']) && !empty($_POST['shopname']) && !empty($_POST['bid']) && !empty($_POST['bname'])){
      $option = array(
        'teid'        => $_POST['teid'],
        'tename'      => $_POST['tename'],
        'tetel'       => $_POST['tetel'],
        'tebirth'     => $_POST['tebirth'],
        'shopid'      => $_POST['shopid'],
        'shopname'    => $_POST['shopname'],
        'bid'         => $_POST['bid'],
        'bname'       => $_POST['bname']
      );
      echo $this->M_terminal->updateTerminal($option);
    }
  }
  public function getTerinalShop(){
    if(!empty($_GET['teid'])){
      $result = $this->M_terminal->getteshop($_GET['teid']);
      print_r(json_encode($result));
    }else{
      echo "false";
    }
  }
  public function getTerinalBrand(){
    if(!empty($_GET['teid'])){
      $result = $this->M_terminal->gettebrand($_GET['teid']);
      print_r(json_encode($result));
    }else{
      echo "false";
    }
  }
  public function addTerminalcallback(){
    if(!empty($_GET['teid']) && !empty($_GET['val'])){
      $flag = $this->M_terminal->addTerminalCallback($_GET['teid'],$_GET['val'],$this->session->userdata('username'));
      // echo $flag;
      // return false;
      if($flag>0){
        echo "true";
      }else {
        echo "false";
      }
    }else{
      echo "false";
    }
  }
  public function deleteTerminalcallback(){
    if(!empty($_POST['id'])){
      $flag = $this->M_terminal->deleteTerminalcallback($_POST['id']);
      if($flag>0){
        echo "true";
      }else {
        echo "false";
      }
    }else{
      echo "false";
    }
  }
  public function updateTerminalcallback(){
    if(!empty($_GET['id']) && !empty($_GET['val'])){
      $flag = $this->M_terminal->updateTerminalcallback($_GET['id'],$_GET['val']);
      if($flag>0){
        echo "true";
      }else {
        echo "false";
      }
    }else{
      echo "false";
    }
  }
  public function terminalcallbacklist(){
    $this->load->library('pagination');
    $page = 0;
    $offset = 0;
    $per_page = 10;
    if(!empty($_GET['per_page'])){
      $page = $_GET['per_page'];
      $offset = ($page-1)*$per_page;
    }
    $result = $this->M_terminal->getMycallback($this->session->userdata('username'),$per_page,$offset);
    if(!empty($_GET['search'])){
      $result = $this->M_terminal->selectMycallback($_GET['search'],$this->session->userdata('username'),$per_page,$offset);
    }
    $config['base_url']=site_url('terminal/terminalcallbacklist');
    $config['total_rows'] = $result['total'];
    $config['per_page'] = $per_page;
    $this->pagination->initialize($config);
    $data['callback'] = $result['callback'];
    $data['pager'] = $this->pagination->create_links();
    $this->load->view('terminalcallbacklist',$data);
    // $this->load->view('terminalcallbacklist');
  }
}
